// index.js
import {
  get,
  post
} from '../../utils/request'
import {
  APIS
} from '../../utils/api'
import { timestampToTime } from '../../utils/util'
// 获取应用实例
const app = getApp()

Page({
  data: {
    shop: '',
    discount: {},
    username: '',
    vipList: [],
    selectId: '',
    id: ''
  },
  onLoad(opt) {
    let shop = wx.getStorageSync('currentShop');
    this.setData({
      shop,
      id: opt.id
    })
    this.getDetail(opt.id);
  },
  getDetail(id) {
    get(`${APIS.discount}/${id}`)
      .then(res => {
        this.setData({
          discount: res.data.data
        })
      })
  },
  handleInput(e) {
    this.setData({
      username: e.detail.value
    })
  },
  handleSelect(e) {
    let userInfo = e.currentTarget.dataset.info;
    this.setData({
      selectId: userInfo.id === this.data.selectId ? '' : userInfo.id
    })
  },
  handleSearch() {
    let {shop, username} = this.data;
    if (!username) {
      return;
    }
    get(APIS.vip + '/search', {
      shop,
      search: username
    })
      .then(res => {
        let vipList = res.data;
        this.setData({
          selectId: '',
          vipList: vipList
        })
      })
  },
  handleJump(e) {
    let userInfo = e.currentTarget.dataset.info;
    let {currentIndex, currentShopName} = this.data;
    wx.navigateTo({
      url: `/pages/vipDetail/index?id=${userInfo.id}&shop=${currentIndex}&shopname=${currentShopName}`
    })
  },
  handleDist() {
    const {selectId, id} = this.data;
    if (!selectId) {return}
    post(APIS.receive, {
      luid: selectId,
      did: id
    }).then(res => {
      this.setData({
        selectId: ''
      })
      wx.showToast({
        title: res.message,
        icon: 'none'
      })
    })
  }
})